package 剑指Offer;

import java.util.ArrayList;
import java.util.List;

public class Offer54_二叉搜索树的第k大节点 {

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }


    List<Integer> list = new ArrayList<>();

    public int kthLargest(TreeNode root, int k) {
        help(root);

        int size = list.size();
        //第一大，也就是数组最后一个元素，下标应该为size-1 即size-k
        return list.get(size - k);
    }

    //进行中序遍历
    public void help(TreeNode root) {
        if (root == null) {
            return;
        }
        help(root.left);
        list.add(root.val);
        help(root.right);
    }
}
